package com.zvfe.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zvfe.common.QueryPageParam;
import com.zvfe.common.R;
import com.zvfe.pojo.Storage;
import com.zvfe.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wny
 * @since 2023-07-03
 */
@Slf4j
@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    private StorageService storageService;

    //新增
    @PostMapping("/add")
    public R add(@RequestBody Storage storage) {
        return storageService.save(storage) ? R.success() : R.err();
    }

    //根据Id修改修改
    @PutMapping("/update")
    public R updateById(@RequestBody Storage storage) {
        return storageService.updateById(storage) ? R.success() : R.err();
    }

    //根据ID删除
    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable Integer id) {
        return storageService.removeById(id) ? R.success() : R.err();
    }

    //管理员分页查询
    @PostMapping("/listPage")
    public R listPage(@RequestBody QueryPageParam param) {
        HashMap hm = param.getParam();
        Page<Storage> page = new Page<>();
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        String name = (String) hm.get("name");
        LambdaQueryWrapper<Storage> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Storage::getName, name)
                .orderByAsc(Storage::getId);
        IPage<Storage> result = storageService.page(page, wrapper);
        return R.success(result.getRecords(), result.getTotal());
    }

    @GetMapping("/list")
    public R list(){
        List list = storageService.list();
        return R.success(list);
    }

}
